<?php
    @include_once("conn.php");

    $obj = array();

    $user = $_POST["user"];
    $gId = $_POST["gId"];
    $buyNum = $_POST["buyNum"];
 

    if(!($user&&$gId&&$buyNum)){
        $obj["status"] = false;
        $obj["detail"] = "请输入完整的参数";
        exit(json_encode($obj));
    }

    $sql = "select *  from `cart` where user = '$user' and gId = '$gId'";

    $result = mysqli_query($conn,$sql);

    $item = mysqli_fetch_assoc($result);

    if($item){
        $sql = "update `cart` set buyNum = cart.buyNum + $buyNum where user = '$user' and gId = '$gId'";
    }else{
        $sql = "insert into `cart`(user,gId,buyNum) values('$user','$gId','$buyNum')";
    }
    $bool = mysqli_query($conn,$sql);

    if(!$bool){
        $obj["status"] = false;
        $obj["detail"] = "fail";
        $obj["sql"] = $sql;
        exit(json_encode($obj));
    }

    $rows = mysqli_affected_rows($conn);

    if($rows>0){
        $obj["status"] = true;
        $obj["detail"] = "注册成功";
        exit(json_encode($obj));
    }

?>